﻿using System.ComponentModel;
using System.Reflection;

namespace ThresholdAssistant.DataContract.Extensions
{
    /// <summary>
    /// 自定义枚举扩展
    /// </summary>
    static class EnumExtensions
    {
        /// <summary>
        /// 自定义枚举扩展
        /// </summary>
        /// <param name="value">定义枚举</param>
        /// <param name="param">参数名称</param>
        /// <returns></returns>
        public static string GetDescription(this System.Enum value, string param = "")
        {
            var valueStr = "";
            if (value == null)
            {
                return valueStr;
            }

            FieldInfo field = value.GetType().GetField(value.ToString());
            if (field == null)
            {
                return valueStr;
            }

            var attribute = field.GetCustomAttributes(typeof(DescriptionAttribute), true);
            if (attribute != null & attribute.Length > 0)
            {
                return (attribute[0] is DescriptionAttribute descriptionAttribute) ? $"{param}{descriptionAttribute.Description}" : "";
            }
            return valueStr;
        }
    }
}